//
//  UIBarButtonItem+Extension.swift
//  Weibo
//
//  Created by HM on 16/5/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    //Designated 专门设计的,指定的构造函数
    //扩展自定义的构造方法
    //给函数的参数设置默认值
    //函数的参数的默认值 如果不传递 就直接使用默认值
     convenience init(imageName: String? = nil,title: String? = nil,target: AnyObject?, action: Selector) {
        self.init()
        
        
        let btn = UIButton()
        //设置图片
        if let img = imageName {
            btn.setImage(UIImage(named: img), forState: .Normal)
            btn.setImage(UIImage(named: img + "_highlighted"), forState: .Highlighted)
        }
        
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        btn.setTitle(title, forState: .Normal)
        
        //设置图片的显示范围
        btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -8, bottom: 0, right: 0)
        //设置颜色
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        //设置自适应大小
        btn.sizeToFit()
        
        //设置自定义视图
        self.customView = btn
    }
    
    
    
}
